import { Service } from 'egg';
import * as fs from 'fs';
import * as path from 'path';

class AvatarService extends Service {
  public async generate(base64: string) {
    base64 = base64.replace(/^data:image\/\w+;base64,/, '');
    const random = Date.now();
    const imagePath = path.resolve(__dirname, '../public/', `${random}.png`);
    const imgBuf = Buffer.from(base64, 'base64');
    fs.writeFileSync(imagePath, imgBuf);
    return `http://127.0.0.1:7001/public/${random}.png`;
  }
}

export default AvatarService;
